/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package robotics.robots;

import robotics.robots.IRobot;
import java.awt.EventQueue;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fallen
 */
public class ThreadedRobot implements IRobot {
    /** inner interpretador */
    IRobot robot;

    public ThreadedRobot(IRobot interpr) {
        this.robot = interpr;
    }
    
    // <editor-fold desc="thread related stuff" defaultstate="collapsed">
    @Override
    public void straight(final double distance) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    robot.straight(distance);
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        robot.stop();
                    } catch (RemoteException ex) {
                        Logger.getLogger(ThreadedRobot.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
    }

    @Override
    public void curveLeft(final double radius, final double angle) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    robot.curveLeft(radius, angle);
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        robot.stop();
                    } catch (RemoteException ex) {
                        Logger.getLogger(ThreadedRobot.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
    }

    @Override
    public void curveRight(final double radius, final double angle) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    robot.curveRight(radius, angle);
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        robot.stop();
                    } catch (RemoteException ex) {
                        Logger.getLogger(ThreadedRobot.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
    }
    // </editor-fold>

    @Override
    public void setOffsetR(double offsetl) {
        robot.setOffsetR(offsetl);
    }

    @Override
    public void setOffsetL(double offsetl) {
        robot.setOffsetL(offsetl);
    }

    @Override
    public void stop() throws RemoteException {
        robot.stop();
    }

    @Override
    public void setDebug(Boolean debug) {
        robot.setDebug(debug);
    }

    @Override
    public void log(String str) {
        robot.log(str);
    }
}
